library(tidyverse) # for data cleaning and plotting
## ── Attaching packages ─────────────────────────────────────── tidyverse 1.3.0 ──
## ✓ ggplot2 3.3.3 ✓ purrr 0.3.4
## ✓ tibble 3.0.5 ✓ dplyr 1.0.3
## ✓ tidyr 1.1.2 ✓ stringr 1.4.0
## ✓ readr 1.4.0 ✓ forcats 0.5.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
library(lubridate) # for date manipulation
##
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
##
## date, intersect, setdiff, union
library(openintro) # for the abbr2state() function
## Loading required package: airports
## Loading required package: cherryblossom
## Loading required package: usdata
library(palmerpenguins)# for Palmer penguin data
library(maps) # for map data
##
## Attaching package: 'maps'
## The following object is masked from 'package:purrr':
##
## map
library(ggmap) # for mapping points on maps
## Google's Terms of Service: https://cloud.google.com/maps-platform/terms/.
## Please cite ggmap if you use it! See citation("ggmap") for details.
library(gplots) # for col2hex() function
##
## Attaching package: 'gplots'
## The following object is masked from 'package:stats':
##
## lowess
library(RColorBrewer) # for color palettes
library(sf) # for working with spatial data
## Linking to GEOS 3.8.1, GDAL 3.1.4, PROJ 6.3.1
library(leaflet) # for highly customizable mapping
library(carData) # for Minneapolis police stops data
library(ggthemes) # for more themes (including theme_map())
theme_set(theme_minimal())
# Starbucks locations
Starbucks <- read_csv("https://www.macalester.edu/~ajohns24/Data/Starbucks.csv")
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## Brand = col_character(),
## `Store Number` = col_character(),
## `Store Name` = col_character(),
## `Ownership Type` = col_character(),
## `Street Address` = col_character(),
## City = col_character(),
## `State/Province` = col_character(),
## Country = col_character(),
## Postcode = col_character(),
## `Phone Number` = col_character(),
## Timezone = col_character(),
## Longitude = col_double(),
## Latitude = col_double()
## )
starbucks_us_by_state <- Starbucks %>%
filter(Country == "US") %>%
count(`State/Province`) %>%
mutate(state_name = str_to_lower(abbr2state(`State/Province`)))
# Lisa's favorite St. Paul places - example for you to create your own data
favorite_stp_by_lisa <- tibble(
place = c("Home", "Macalester College", "Adams Spanish Immersion",
"Spirit Gymnastics", "Bama & Bapa", "Now Bikes",
"Dance Spectrum", "Pizza Luce", "Brunson's"),
long = c(-93.1405743, -93.1712321, -93.1451796,
-93.1650563, -93.1542883, -93.1696608,
-93.1393172, -93.1524256, -93.0753863),
lat = c(44.950576, 44.9378965, 44.9237914,
44.9654609, 44.9295072, 44.9436813,
44.9399922, 44.9468848, 44.9700727)
)
#COVID-19 data from the New York Times
covid19 <- read_csv("https://raw.githubusercontent.com/nytimes/covid-19-data/master/us-states.csv")
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## date = col_date(format = ""),
## state = col_character(),
## fips = col_character(),
## cases = col_double(),
## deaths = col_double()
## )
If you were not able to get set up on GitHub last week, go here and get set up first. Then, do the following (if you get stuck on a step, don’t worry, I will help! You can always get started on the homework and we can figure out the GitHub piece later):
keep_md: TRUE in the YAML heading. The .md file is a markdown (NOT R Markdown) file that is an interim step to creating the html file. They are displayed fairly nicely in GitHub, so we want to keep it and look at it there. Click the boxes next to these two files, commit changes (remember to include a commit message), and push them (green up arrow).Put your name at the top of the document.
For ALL graphs, you should include appropriate labels.
Feel free to change the default theme, which I currently have set to theme_minimal().
Use good coding practice. Read the short sections on good code with pipes and ggplot2. This is part of your grade!
When you are finished with ALL the exercises, uncomment the options at the top so your document looks nicer. Don’t do it before then, or else you might miss some important warnings and messages.
These exercises will reiterate what you learned in the “Mapping data with R” tutorial. If you haven’t gone through the tutorial yet, you should do that first.
ggmap)Starbucks locations to a world map. Add an aesthetic to the world map that sets the color of the points according to the ownership type. What, if anything, can you deduce from this visualization?world <- get_stamenmap(
bbox = c(left = -180, bottom = -57, right = 179, top = 82.1),
maptype = "terrain",
zoom = 2)
## Source : http://tile.stamen.com/terrain/2/0/0.png
## Source : http://tile.stamen.com/terrain/2/1/0.png
## Source : http://tile.stamen.com/terrain/2/2/0.png
## Source : http://tile.stamen.com/terrain/2/3/0.png
## Source : http://tile.stamen.com/terrain/2/0/1.png
## Source : http://tile.stamen.com/terrain/2/1/1.png
## Source : http://tile.stamen.com/terrain/2/2/1.png
## Source : http://tile.stamen.com/terrain/2/3/1.png
## Source : http://tile.stamen.com/terrain/2/0/2.png
## Source : http://tile.stamen.com/terrain/2/1/2.png
## Source : http://tile.stamen.com/terrain/2/2/2.png
## Source : http://tile.stamen.com/terrain/2/3/2.png
ggmap(world) +
geom_point(data = Starbucks,
aes(x = Longitude, y = Latitude, color = `Ownership Type`),
alpha = .3,
size = .2) +
theme_map()
## Warning: Removed 1 rows containing missing values (geom_point).
twincities <- get_stamenmap(
bbox = c(left = -93.6, bottom = 44.8, right = -92.8, top = 45.1),
maptype = "terrain",
zoom = 11)
## Source : http://tile.stamen.com/terrain/11/491/735.png
## Source : http://tile.stamen.com/terrain/11/492/735.png
## Source : http://tile.stamen.com/terrain/11/493/735.png
## Source : http://tile.stamen.com/terrain/11/494/735.png
## Source : http://tile.stamen.com/terrain/11/495/735.png
## Source : http://tile.stamen.com/terrain/11/496/735.png
## Source : http://tile.stamen.com/terrain/11/491/736.png
## Source : http://tile.stamen.com/terrain/11/492/736.png
## Source : http://tile.stamen.com/terrain/11/493/736.png
## Source : http://tile.stamen.com/terrain/11/494/736.png
## Source : http://tile.stamen.com/terrain/11/495/736.png
## Source : http://tile.stamen.com/terrain/11/496/736.png
## Source : http://tile.stamen.com/terrain/11/491/737.png
## Source : http://tile.stamen.com/terrain/11/492/737.png
## Source : http://tile.stamen.com/terrain/11/493/737.png
## Source : http://tile.stamen.com/terrain/11/494/737.png
## Source : http://tile.stamen.com/terrain/11/495/737.png
## Source : http://tile.stamen.com/terrain/11/496/737.png
## Source : http://tile.stamen.com/terrain/11/491/738.png
## Source : http://tile.stamen.com/terrain/11/492/738.png
## Source : http://tile.stamen.com/terrain/11/493/738.png
## Source : http://tile.stamen.com/terrain/11/494/738.png
## Source : http://tile.stamen.com/terrain/11/495/738.png
## Source : http://tile.stamen.com/terrain/11/496/738.png
ggmap(twincities) +
geom_point(data = Starbucks,
aes(x = Longitude, y = Latitude),
alpha = .8) +
theme_map() +
labs(title= "Starbucks in the Twin Cities Metro")
## Warning: Removed 25487 rows containing missing values (geom_point).
A larger zoom number allows you to see more area, but with less details. You aren’t able to see the names of cities or anything. On the flip side, you should be able to see more detail of a smaller area with a small number, but for some reason my map is very blurry when I zoom in.
get_stamenmap() in help and look at maptype). Include a map with one of the other map types.twincities <- get_stamenmap(
bbox = c(left = -93.6, bottom = 44.8, right = -92.8, top = 45.1),
maptype = "watercolor",
zoom = 11)
## Source : http://tile.stamen.com/watercolor/11/491/735.jpg
## Source : http://tile.stamen.com/watercolor/11/492/735.jpg
## Source : http://tile.stamen.com/watercolor/11/493/735.jpg
## Source : http://tile.stamen.com/watercolor/11/494/735.jpg
## Source : http://tile.stamen.com/watercolor/11/495/735.jpg
## Source : http://tile.stamen.com/watercolor/11/496/735.jpg
## Source : http://tile.stamen.com/watercolor/11/491/736.jpg
## Source : http://tile.stamen.com/watercolor/11/492/736.jpg
## Source : http://tile.stamen.com/watercolor/11/493/736.jpg
## Source : http://tile.stamen.com/watercolor/11/494/736.jpg
## Source : http://tile.stamen.com/watercolor/11/495/736.jpg
## Source : http://tile.stamen.com/watercolor/11/496/736.jpg
## Source : http://tile.stamen.com/watercolor/11/491/737.jpg
## Source : http://tile.stamen.com/watercolor/11/492/737.jpg
## Source : http://tile.stamen.com/watercolor/11/493/737.jpg
## Source : http://tile.stamen.com/watercolor/11/494/737.jpg
## Source : http://tile.stamen.com/watercolor/11/495/737.jpg
## Source : http://tile.stamen.com/watercolor/11/496/737.jpg
## Source : http://tile.stamen.com/watercolor/11/491/738.jpg
## Source : http://tile.stamen.com/watercolor/11/492/738.jpg
## Source : http://tile.stamen.com/watercolor/11/493/738.jpg
## Source : http://tile.stamen.com/watercolor/11/494/738.jpg
## Source : http://tile.stamen.com/watercolor/11/495/738.jpg
## Source : http://tile.stamen.com/watercolor/11/496/738.jpg
ggmap(twincities)
annotate() function (see ggplot2 cheatsheet).twincities <- get_stamenmap(
bbox = c(left = -93.6, bottom = 44.8, right = -92.8, top = 45.1),
maptype = "terrain",
zoom = 11)
ggmap(twincities) +
geom_point(data = Starbucks,
aes(x = Longitude, y = Latitude),
alpha = .8) +
theme_map() +
annotate(geom = "text", x = -93.1691, y = 44.9379, label = "Macalester") +
annotate(geom = "point", x = -93.1691, y = 44.9379, color = "red") +
labs(title = "Location of Macalester College in the Twin Cities")
## Warning: Removed 25487 rows containing missing values (geom_point).
geom_map())The example I showed in the tutorial did not account for population of each state in the map. In the code below, a new variable is created, starbucks_per_10000, that gives the number of Starbucks per 10,000 people. It is in the starbucks_with_2018_pop_est dataset.
census_pop_est_2018 <- read_csv("https://www.dropbox.com/s/6txwv3b4ng7pepe/us_census_2018_state_pop_est.csv?dl=1") %>%
separate(state, into = c("dot","state"), extra = "merge") %>%
select(-dot) %>%
mutate(state = str_to_lower(state))
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## state = col_character(),
## est_pop_2018 = col_double()
## )
starbucks_with_2018_pop_est <-
starbucks_us_by_state %>%
left_join(census_pop_est_2018,
by = c("state_name" = "state")) %>%
mutate(starbucks_per_10000 = (n/est_pop_2018)*10000)
dplyr review: Look through the code above and describe what each line of code does.169 assigns a name to the dataset that is being read in from a website below and is being modified 170 takes the dot out of the values in the “state” column and makes a new column 171 gets rid of the dot column just created 172 creates a new column where the state names are now lowercases (helps w/ joining) 174 assigns a name to the modified dataset below 175 introduces the data we’ll be modifying and pipes it into the next code 176 merges the two datasets, with the list specfiying that two columns that have different names are actually showing the same data. 177 creates a new variable in which a proportion is created.
I observe that, unsurprisingly, Starbucks are concentrated in urban areas. Highly populated Western states have a higher proportion of Starbucks. On the Eastern half of the US, while it looks like there are many Starbucks locations, the concentration compared the total population is not as large.
UnitedStates <- map_data("state")
starbucks_with_2018_pop_est %>%
ggplot() +
geom_map(map = UnitedStates,
aes(map_id = state_name,
fill = starbucks_per_10000)) +
scale_color_hue(direction = -1) +
expand_limits(x = UnitedStates$long, y = UnitedStates$lat) +
theme_map() +
geom_point(data = Starbucks %>% filter(`Country` == "US", `State/Province` != "AK", `State/Province` != "HI"),
aes(x = Longitude, y = Latitude),
size = .05,
alpha = .2,
color = "goldenrod") +
labs(title = "Location and Proportion of Starbucks by State",
caption = "by Aldric Martinez-Olson")
leaflet)tibble() function that has 10-15 rows of your favorite places. The columns will be the name of the location, the latitude, the longitude, and a column that indicates if it is in your top 3 favorite locations or not. For an example of how to use tibble(), look at the favorite_stp_by_lisa I created in the data R code chunk at the beginning.favorite_stp_by_Aldric <- tibble(
place = c("Minnehaha Falls", "Macalester College", "Shadow Falls",
"Everyday People", "Moscow on the Hill", "Mill City",
"Kowalski's", "Midtown Global Market", "Tiny Diner", "Industrial Bridge"),
long = c(-93.210983, -93.1712321, -93.1977677,
-93.1678257, -93.11556243896484, -93.2566158,
-93.1536278, -93.26776, -93.2590105, -93.271466),
lat = c(44.9153316, 44.9378965, 44.9425081,
44.9466433, 44.946800231933594, 44.9782951,
44.9401052, 44.948352, 44.9344555, 45.004447),
top3 = c(FALSE, FALSE, TRUE, FALSE, TRUE, FALSE, FALSE, FALSE, FALSE, TRUE)
)
favorite_stp_by_Aldric %>%
arrange(long)
leaflet map that uses circles to indicate your favorite places. Label them with the name of the place. Choose the base map you like best. Color your 3 favorite places differently than the ones that are not in your top 3 (HINT: colorFactor()). Add a legend that explains what the colors mean.leaflet(data = favorite_stp_by_Aldric %>% arrange(long)) %>%
addTiles() %>%
addCircles(lng = ~long,
lat = ~lat,
label = ~place) %>%
addPolylines(lng = ~long,
lat = ~lat)
Connect all your locations together with a line in a meaningful way (you may need to order them differently in the original data).
If there are other variables you want to add that could enhance your plot, do that now.
This section will revisit some datasets we have used previously and bring in a mapping component.
The data come from Washington, DC and cover the last quarter of 2014.
Two data tables are available:
Trips contains records of individual rentalsStations gives the locations of the bike rental stationsHere is the code to read in the data. We do this a little differently than usually, which is why it is included here rather than at the top of this file. To avoid repeatedly re-reading the files, start the data import chunk with {r cache = TRUE} rather than the usual {r}. This code reads in the large dataset right away.
data_site <-
"https://www.macalester.edu/~dshuman1/data/112/2014-Q4-Trips-History-Data.rds"
Trips <- readRDS(gzcon(url(data_site)))
Stations<-read_csv("http://www.macalester.edu/~dshuman1/data/112/DC-Stations.csv")
##
## ── Column specification ────────────────────────────────────────────────────────
## cols(
## name = col_character(),
## lat = col_double(),
## long = col_double(),
## nbBikes = col_double(),
## nbEmptyDocks = col_double()
## )
Stations to make a visualization of the total number of departures from each station in the Trips data. Use either color or size to show the variation in number of departures. This time, plot the points on top of a map. Use any of the mapping tools you’d like.DCmap <- get_stamenmap(
bbox = c(left = -77.1320, bottom = 38.8665, right = -76.9450, top = 38.9556),
maptype = "terrain",
zoom = 13)
## Source : http://tile.stamen.com/terrain/13/2340/3132.png
## Source : http://tile.stamen.com/terrain/13/2341/3132.png
## Source : http://tile.stamen.com/terrain/13/2342/3132.png
## Source : http://tile.stamen.com/terrain/13/2343/3132.png
## Source : http://tile.stamen.com/terrain/13/2344/3132.png
## Source : http://tile.stamen.com/terrain/13/2345/3132.png
## Source : http://tile.stamen.com/terrain/13/2340/3133.png
## Source : http://tile.stamen.com/terrain/13/2341/3133.png
## Source : http://tile.stamen.com/terrain/13/2342/3133.png
## Source : http://tile.stamen.com/terrain/13/2343/3133.png
## Source : http://tile.stamen.com/terrain/13/2344/3133.png
## Source : http://tile.stamen.com/terrain/13/2345/3133.png
## Source : http://tile.stamen.com/terrain/13/2340/3134.png
## Source : http://tile.stamen.com/terrain/13/2341/3134.png
## Source : http://tile.stamen.com/terrain/13/2342/3134.png
## Source : http://tile.stamen.com/terrain/13/2343/3134.png
## Source : http://tile.stamen.com/terrain/13/2344/3134.png
## Source : http://tile.stamen.com/terrain/13/2345/3134.png
Stations_Trips <- Trips %>%
left_join(Stations, by = c("sstation" = "name")) %>%
group_by(lat, long, sstation) %>%
summarize(departures = n())
## `summarise()` has grouped output by 'lat', 'long'. You can override using the `.groups` argument.
ggmap(DCmap) +
geom_point(data = Stations_Trips,
aes(x = long, y = lat, size = departures),
alpha = .8) +
labs(title = "Popular Bike Stations in the DC Area")
## Warning: Removed 124 rows containing missing values (geom_point).
I noticed that in the residential neighborhoods have a lot of registered riders, while the stations by the parks/river/recreation area have a lot of casual riders. This is probably because tourists who only need a short time are going to be riding by the recreation areas.
ggmap(DCmap) +
geom_point(data = Stations_Trips,
aes(x = long, y = lat, size = departures),
alpha = .8)
## Warning: Removed 124 rows containing missing values (geom_point).
Stations %>%
left_join(Trips, by = c("name" = "sstation")) %>%
group_by(lat, long, name) %>%
summarize(propcas = mean(client == "Casual"))
## `summarise()` has grouped output by 'lat', 'long'. You can override using the `.groups` argument.
ggmap(DCmap) +
geom_point(data = Stations %>%
left_join(Trips, by = c("name" = "sstation")) %>%
group_by(lat, long, name) %>%
summarize(propcas = mean(client == "Casual")),
aes(x = long, y = lat, color = propcas)) +
labs(title = "Casual Ridership at Bike Stations in the DC Area")
## `summarise()` has grouped output by 'lat', 'long'. You can override using the `.groups` argument.
## Warning: Removed 117 rows containing missing values (geom_point).
The following exercises will use the COVID-19 data from the NYT.
I see that most of the least populated states are purple while the the most populated states are orange, yellow, and reddish-pink. It does not take into account the covid cases relative to the population of the state, so small states will always be purple.
CovidCases <- covid19 %>%
group_by(state) %>%
summarize(recent_cases = max(cases)) %>%
mutate(state_name = str_to_lower(state))
UnitedStates <- map_data("state")
CovidCases %>%
ggplot() +
geom_map(map = UnitedStates,
aes(map_id = state_name,
fill = recent_cases)) +
scale_fill_viridis_c(option = "C") +
expand_limits(x = UnitedStates$long, y = UnitedStates$lat) +
theme_map() +
theme(legend.position="right") +
labs(title = "Recent Cumulative Cases in the United States")
CovidCases_Pop <- covid19 %>%
group_by(state) %>%
summarize(recent_cases = max(cases)) %>%
mutate(state_name = str_to_lower(state)) %>%
left_join(census_pop_est_2018, by = c("state_name" = "state")) %>%
mutate(cases_per_10000 = (recent_cases/est_pop_2018)*10000)
CovidCases_Pop %>%
ggplot() +
geom_map(map = UnitedStates,
aes(map_id = state_name,
fill = cases_per_10000)) +
scale_fill_viridis_c(option = "C") +
expand_limits(x = UnitedStates$long, y = UnitedStates$lat) +
theme_map() +
theme(legend.position="right") +
labs(title = "Covid Infection Proportion in US States")
These exercises use the datasets MplsStops and MplsDemo from the carData library. Search for them in Help to find out more information.
MplsStops dataset to find out how many stops there were for each neighborhood and the proportion of stops that were for a suspicious vehicle or person. Sort the results from most to least number of stops. Save this as a dataset called mpls_suspicious and display the table.mpls_suspicious <- MplsStops %>%
group_by(neighborhood) %>%
mutate(neighborhood_stops = n()) %>%
mutate(prop_stops = mean(problem == "suspicious")) %>%
select(neighborhood, neighborhood_stops, prop_stops) %>%
arrange(desc(neighborhood_stops)) %>%
distinct()
mpls_suspicious
leaflet map and the MplsStops dataset to display each of the stops on a map as a small point. Color the points differently depending on whether they were for suspicious vehicle/person or a traffic stop (the problem variable). HINTS: use addCircleMarkers, set stroke = FAlSE, use colorFactor() to create a palette.MplsStops
pal <- colorFactor("viridis",
domain = MplsStops$problem)
leaflet(data = MplsStops) %>%
addTiles() %>%
addCircleMarkers(lng = ~long,
lat = ~lat,
radius = 1.5,
opacity = .5,
color = ~pal(problem),
stroke = FALSE) %>%
addLegend(pal = pal,
values = ~problem,
title = ~"Type of Stop",
position = "bottomright")
eval=FALSE. Although it looks like it only links to the .sph file, you need the entire folder of files to create the mpls_nbhd data set. These data contain information about the geometries of the Minneapolis neighborhoods. Using the mpls_nbhd dataset as the base file, join the mpls_suspicious and MplsDemo datasets to it by neighborhood (careful, they are named different things in the different files). Call this new dataset mpls_all.mpls_nbhd <- st_read("Minneapolis_Neighborhoods/Minneapolis_Neighborhoods.shp", quiet = TRUE)
MplsDemo
mpls_all <- mpls_nbhd %>%
left_join(mpls_suspicious, by = c("BDNAME" = "neighborhood")) %>%
left_join(MplsDemo, by = c("BDNAME" = "neighborhood"))
leaflet to create a map from the mpls_all data that colors the neighborhoods by prop_suspicious. Display the neighborhood name as you scroll over it. Describe what you observe in the map.It seems that there are more of stops for suspicious activity in the south east of Minneapolis to downtown. The boundaries for the areas of suspicious stops is cutoff by the highway and river. There are also stops for suspicious activity in the northwest and southwest corner. There are more traffic stops in the north and to the west of the highway.
pal2 <- colorNumeric("viridis",
domain = mpls_suspicious$prop_stops)
leaflet(data = mpls_all) %>%
addTiles() %>%
addPolygons(fillColor = ~pal2(prop_stops),
fillOpacity = 0.6,
stroke = FALSE,
label = ~paste(str_to_title(BDNAME))) %>%
addLegend(pal = pal2,
values = ~prop_stops,
opacity = 0.6,
title = ~"Proportion of Stops",
position = "bottomright")
leaflet to create a map of your own choosing. Come up with a question you want to try to answer and use the map to help answer that question. Describe what your map shows.I would like to see how the neighborhoods stops compare to the household income of the neighborhood.
The areas with high income (yellow) and medium-high income (green) have less crime than any part of the city. The areas with less income (dark purple) have more crime concentrated within them. Most of the crime happens in downtown and the area just south of it. There are only two dark purple areas without crime, and these seem to be both on the river, one in the north and one west-central.
pal3 <- colorNumeric("viridis",
domain = mpls_all$hhIncome)
leaflet(data = mpls_all) %>%
addTiles() %>%
addPolygons(fillColor = ~pal3(hhIncome),
fillOpacity = 0.6,
stroke = FALSE,
label = ~paste(str_to_title(BDNAME))) %>%
addCircleMarkers(data = MplsStops,
lng = ~long,
lat = ~lat,
radius = .5,
opacity = .5,
stroke = FALSE)
DID YOU REMEMBER TO UNCOMMENT THE OPTIONS AT THE TOP?